package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 关联查询
 */
public class JDBCDemo11 {
    public static void main(String[] args) {
        //查看王克晶所带班级的信息
        try (Connection connection = DBUtil.getConnection();){
            /*
                SELECT t.name,t.salary,t.gender,t.title,c.name,c.floor
                FROM teacher t
                JOIN class c ON t.id=c.teacher_id
                WHERE t.name=?
             */
            String sql = "SELECT t.name,t.salary,t.gender,t.title,c.name,c.floor " +
                         "FROM teacher t " +
                         "JOIN class c ON t.id=c.teacher_id " +
                         "WHERE t.name=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,"王克晶");
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
//                String tName = rs.getString("t.name");
//                int tSalary = rs.getInt("t.salary");
//                String tGender = rs.getString("t.gender");
//                String tTitle = rs.getString("t.title");
//                String cName = rs.getString("c.name");
//                int cFloor = rs.getInt("c.floor");
                String tName = rs.getString(1);
                int tSalary = rs.getInt(2);
                String tGender = rs.getString(3);
                String tTitle = rs.getString(4);
                String cName = rs.getString(5);
                int cFloor = rs.getInt(6);
                System.out.println(tName+","+tSalary+","+tGender+","+tTitle+","+cName+","+cFloor);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}







